﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Monster : MonoBehaviour
{
    public int Hp, MaxHp, MinHp, RealHp;
    public Text FirstNumText, SecondNumText, ThirdNumText, FourthNumText;
    public float JiShiQi1;
    public int Add, Only;
    public int MuchTooLow, YiFuHuo, FuHuoJiaLi;
    public GameObject Wall, gamepanel, Place0, Place1;
    public bool IsFirstClone;
    public Animator ani;
    public int Level;
    public GameObject player;

    private void Awake()
    {
        Level = DataManager.Instance.Lv + 10000000;
        GetAllData(Level);
        gamepanel = GameObject.FindGameObjectWithTag("GamePanel");
        Place0 = GameObject.FindGameObjectWithTag("Place0");
        Place1 = GameObject.FindGameObjectWithTag("Place1");
        player = GameObject.Find("Player");
    }

    //private void OnEnable()
    //{
    //    GetAllData(Level);
    //    Add = 0;
    //    Only = 0;
    //    MuchTooLow = 0;
    //    YiFuHuo = 0;
    //    FuHuoJiaLi = 0;
    //    IsFirstClone = false;
    //    isok = true;
    //}
    void Start()
    {
        if (!IsFirstClone)
        {
            GetComponent<Rigidbody2D>().gravityScale = 0;
        }
    }


    /// <summary>
    /// 获取怪物的血量范围和数量
    /// </summary>
    /// <param name="Round"></param>
    public void GetAllData(int Round)
    {
        LanguageCfgRowData data = DataManager.Instance.HLLanguageCfg.datas[Round] as LanguageCfgRowData;
        MaxHp = data.MaxHp;
        MinHp = data.MinHp;
        if (!IsFirstClone)
        {
            Hp = Random.Range(MinHp, MaxHp);
            FirstNumText.text = Hp.ToString();
            SecondNumText.text = Hp.ToString();
            ThirdNumText.text = Hp.ToString();
            FourthNumText.text = Hp.ToString();
        }
        RealHp = Hp;
    }

   

  
    void Update()
    {
        if (!GamePanel.instance.IsDeath)
        {
            if (!IsFirstClone)
            {
                if (transform.parent.gameObject.name.Equals("Place0"))
                {
                    JiShiQi1 += Time.deltaTime;
                    if (JiShiQi1 < 1.3f)
                    {
                        transform.Translate(6, 0, 0);
                    }
                    else
                    {
                        GetComponent<Rigidbody2D>().gravityScale = 60;
                        Add++;
                    }

                }
                else if (transform.parent.gameObject.name.Equals("Place1"))
                {
                    JiShiQi1 += Time.deltaTime;
                    if (JiShiQi1 < 1.3f)
                    {
                        transform.Translate(-6, 0, 0);
                    }
                    else
                    {
                        GetComponent<Rigidbody2D>().gravityScale = 60;
                        Add++;
                    }

                }
                else
                {
                    GetComponent<Rigidbody2D>().gravityScale = 60;
                    Add++;
                }

                if (Add == 1)
                {
                    if (transform.parent.gameObject.name.Equals("Place0"))
                    {

                        GetComponent<Rigidbody2D>().AddForce(new Vector3(90, 0, 0) * 2, ForceMode2D.Impulse);
                    }
                    else if (transform.parent.gameObject.name.Equals("Place1"))
                    {

                        GetComponent<Rigidbody2D>().AddForce(new Vector3(-90, 0, 0) * 2, ForceMode2D.Impulse);
                    }
                    else
                    {
                        switch (Random.Range(0, 2))
                        {
                            case 0:
                                GetComponent<Rigidbody2D>().AddForce(new Vector3(-90, 0, 0) * 2, ForceMode2D.Impulse);
                                break;
                            case 1:
                                GetComponent<Rigidbody2D>().AddForce(new Vector3(90, 0, 0) * 2, ForceMode2D.Impulse);
                                break;
                        }
                    }
                }


            }
            else
            {
                GetComponent<Rigidbody2D>().gravityScale = 60;
            }
        }
        else
        {
          
            player.GetComponent<BoxCollider2D>().enabled = false;

        }
    }




    /// <summary>
    /// /与墙壁碰撞
    /// </summary>
    /// <param name="collision"></param>

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.name.Equals("Wall"))
        {

            if (MuchTooLow == 1)
            {

                GetComponent<Rigidbody2D>().AddForce(Vector3.up * 200, ForceMode2D.Impulse);
                MuchTooLow = 0; 
            }
            if (MuchTooLow == 2)
            {
                GetComponent<Rigidbody2D>().AddForce(Vector3.up * 400, ForceMode2D.Impulse);
                MuchTooLow = 0;
            }
           
        }

    }
    bool isok = true;
    /// <summary>
    /// 掉血方法
    /// </summary>
    public void LoseHp()
    {
        Hp -= MainPanel.instance.Hurt;
        FirstNumText.text = Hp.ToString();

        if (Hp <= 0 && isok)
        {
            ZhenDong();
            isok = false;
            ShortVoice.instance.PlayVoice("BaoZha");
            CloneGuangQuan();
            DataManager.Instance.XiaoMieNum += 1;
            DataManager.Instance.Save();
            Only += 1;
            if (gameObject.tag.Equals("First"))
            {
                //GameTools.instance.BackObj(gameObject, Balloon.First);
                Destroy(gameObject);
                GameObject obj = GameTools.instance.GetGameobject(Balloon.SmallGold);
                obj.transform.SetParent(gamepanel.transform);
                obj.transform.position = gameObject.transform.position;
                obj.GetComponent<Rigidbody2D>().AddForce(new Vector3(Random.Range(-90, 90), -90, 0) * 2, ForceMode2D.Impulse);
                if (!gameObject.GetComponent<Monster>().IsFirstClone)
                {
                    if (Only == 1)
                    {

                        GamePanel.instance.AddScore(1);
                        GamePanel.instance.AddMaxScore((int)((Balloon)System.Enum.Parse(typeof(Balloon), gameObject.tag) + 1));

                    }
                }
            }
            else
            {

                FenLie(gameObject);
            }
            if (!gameObject.GetComponent<Monster>().IsFirstClone)
            {
                if (Only == 1)
                {
                    GamePanel.instance.JinDu();
                }
            }
        }
    }
    /// <summary>
    ///分裂调用的方法,将标签转成枚举再转成整形再转枚举 
    /// </summary>
    /// <param name="obj"></param>
    public void FenLie(GameObject obj)
    {
        Balloon color = (Balloon)System.Enum.Parse(typeof(Balloon), obj.tag);

        for (int i = -1; i < 2; i += 2)
        {
            GameObject Obj = GameTools.instance.GetGameobject((Balloon)System.Enum.ToObject(typeof(Balloon), (int)color - 1));
            Obj.GetComponent<Monster>().IsFirstClone = true;
            Obj.transform.SetParent(gamepanel.transform);
            Obj.transform.position = gameObject.transform.position + new Vector3(60 * i, 0, 0);
            if (Obj.transform.localPosition.y < -300)
            {
                Obj.GetComponent<Monster>().MuchTooLow = 1;
            }
            if (Obj.transform.localPosition.y < -500)
            {
                Obj.GetComponent<Monster>().MuchTooLow = 2;
            }
            Obj.GetComponent<Rigidbody2D>().AddForce(new Vector3(90 * i, -90, 0) * 2, ForceMode2D.Impulse);
            if (RealHp % 2 != 0)
            {
                Obj.GetComponent<Monster>().Hp = RealHp / 2 + 1;

            }
            else
            {
                Obj.GetComponent<Monster>().Hp = RealHp / 2;
            }

            Obj.transform.GetChild(0).GetComponent<Text>().text = Obj.GetComponent<Monster>().Hp.ToString();


        }

        Destroy(gameObject);
        //GameTools.instance.BackObj(gameObject, (Balloon)System.Enum.Parse(typeof(Balloon), gameObject.tag));
        if (!gameObject.GetComponent<Monster>().IsFirstClone)
        {
            if (Only == 1)
            {
                GamePanel.instance.AddScore(1);
                GamePanel.instance.AddMaxScore((int)((Balloon)System.Enum.Parse(typeof(Balloon), gameObject.tag) + 1));
            }


        }





    }
    /// <summary>
    /// 克隆雪渣特效
    /// </summary>
    public void CloneSnow()
    {
        GameObject o = Instantiate(Resources.Load<GameObject>("Snow"));
        o.transform.SetParent(gameObject.transform);
        o.transform.position = transform.position - new Vector3(0, 100, 0);
        o.transform.localScale = gameObject.transform.localScale * 1.2f;
    }
    /// <summary>
    /// /克隆光圈特效
    /// </summary>
    public void CloneGuangQuan()
    {
        GameObject guangquan = Instantiate(Resources.Load<GameObject>("GuangQuan"));
        guangquan.transform.SetParent(gamepanel.transform);
        guangquan.transform.position = gameObject.transform.position;
        guangquan.transform.localScale = gameObject.transform.localScale * 5;
        Destroy(guangquan, 0.5f);
    }
    /// <summary>
    /// 震动
    /// </summary>
    public void ZhenDong()
    {
        if (!GoldAndLv.instance.IsZhenDong)
        {
            
            if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
            {
                Handheld.Vibrate();
            }
        }
       
    }

    /// <summary>
    /// 与子弹碰撞
    /// </summary>
    /// <param name="collision"></param>
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag.Equals("Bullet"))
        {
            CloneSnow();
            GameTools.instance.BackObj(collision.gameObject, Balloon.Bullet);
            LoseHp();

        }
    }




}
